package com.example.youhuo.z_a_onepage.fragment

import android.util.Log
import androidx.core.view.get
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.fragment.app.FragmentManager
import androidx.viewpager.widget.ViewPager
import androidx.viewpager2.widget.ViewPager2
import com.example.mvp.view.BaseFragment
import com.example.myapplication.R
import com.example.youhuo.adapter.Vip_Adapter
import com.example.youhuo.entity.Banner_Entity
import com.example.youhuo.entity.BaseEntity
import com.example.youhuo.entity.Tab_Entity
import com.example.youhuo.mvp.contract.LjlContract
import com.example.youhuo.mvp.model.LjlModel
import com.example.youhuo.mvp.presenter.LjlPresenter
import com.example.youhuo.network.HttpType
import com.google.android.material.tabs.TabLayout


class One_Fragment : BaseFragment<LjlPresenter>(), LjlContract.LjlView {

    private val oneTab: TabLayout by lazy { view1.findViewById<TabLayout>(R.id.one_tab) }

    private val oneVip: ViewPager by lazy { view1.findViewById<ViewPager>(R.id.one_vip) }

    private var arrayList: ArrayList<Map<String, Any>>? = null
    private var vip: Vip_Adapter? = null
    private var fragmentlist: ArrayList<Fragment>? = null


    override fun findlayout(): Int {
        return R.layout.fragment_one_
    }

    override fun initView() {
        arrayList = ArrayList()
        fragmentlist = ArrayList()


        fragmentlist!!.add(BlankFragment())
        fragmentlist!!.add(BlankFragment2())
        vip = Vip_Adapter(childFragmentManager, fragmentlist!!)
        oneVip.adapter = vip


        oneTab.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
            override fun onTabSelected(tab: TabLayout.Tab?) {
                var position = tab!!.position
                var customView = tab.customView
                if (customView == null) {

                }
                oneVip.currentItem = position

            }

            override fun onTabUnselected(tab: TabLayout.Tab?) {
            }

            override fun onTabReselected(tab: TabLayout.Tab?) {
            }

        })
        oneVip.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrolled(
                position: Int,
                positionOffset: Float,
                positionOffsetPixels: Int
            ) {

            }

            override fun onPageSelected(position: Int) {
                oneTab.selectTab(oneTab.getTabAt(position))
            }

            override fun onPageScrollStateChanged(state: Int) {

            }

        })
        //设置banner


    }


    override fun initData() {
        myprefrag = LjlPresenter(LjlModel(), this)

        var map: HashMap<String, Any>? = HashMap()
        map!!.put("code", HttpType.LISTCODE)
        map!!.put("menu", "1")
        arrayList!!.add(map)

//        var bannermap:HashMap<String,Any>?= HashMap()
//        bannermap!!.put("code",HttpType.BANNER)
//        arrayList!!.add(bannermap)
//
//
        myprefrag.TabData(arrayList!!)


    }


    override fun showmsg(entity: BaseEntity) {
        if (entity is Tab_Entity) {
            var tabEntity = entity as Tab_Entity
            var recommend = tabEntity.category
            for (i in 0..recommend!!.size - 1) {
                oneTab.addTab(oneTab.newTab().setText(recommend[i].category_name))
            }
        }

    }


}